package com.dfbz.demo;

import java.util.Random;
import java.util.TreeSet;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo07_TreeSet {

    public static void main(String[] args) {

        TreeSet<Teacher> set = new TreeSet<>();
        set.add(new Teacher("小灰", 20));
        set.add(new Teacher("小蓝", 18));
        set.add(new Teacher("小刘", 16));
        set.add(new Teacher("小静", 22));

        System.out.println(set);
    }

    public static void test(String[] args) {

        TreeSet<String> set = new TreeSet<>();
        set.add("b");
        set.add("c");
        set.add("a");
        set.add("d");

        System.out.println(set);
    }
}

class Teacher implements Comparable {

    @Override
    public int compareTo(Object o) {
        Teacher teacher = (Teacher) o;

        System.out.println("this: " + this + "------------传递进来的对象: " + teacher);

        return this.getAge() - teacher.getAge();
    }


    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}